import { LOGIN_CENTRE, LOGIN_FAIL, LOGIN_SUCCESS } from "../action";
import { produce } from "immer";
import store2 from "store2";

let loginState = {
  login_centre: false,
  login_success: false,
  userInfo: null,
};
let info = store2.get("react-xm");
if (info) {
  loginState.login_success = true;
  loginState.userInfo = info;
}
export const user = (state = loginState, action) => {
  switch (action.type) {
    case LOGIN_CENTRE:
      return produce(state, (cloneState) => {
        cloneState.login_centre = true;
      });
    case LOGIN_SUCCESS:
      return produce(state, (cloneState) => {
        cloneState.login_centre = false;
        cloneState.login_success = true;
        cloneState.userInfo = action.userInfo;
        store2.set("react-xm", action.userInfo);
      });
    case LOGIN_FAIL:
      return produce(state, (cloneState) => {
        cloneState.login_centre = false;
        cloneState.login_success = false;
        cloneState.userInfo = null;
        store2.remove("react-xm");
      });
    default:
      return state;
  }
};
